package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;


    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//        //1.查询总记录数
//        Long count = empMapper.count();
//        //2.分页查询
//        List<Emp> page1 = empMapper.Page((page - 1)*pageSize, pageSize);
//
//        PageBean pageBean = new PageBean(count, page1);
//        return pageBean;
//    }

    public PageBean page(Integer page, Integer pageSize , String name, Short gender, LocalDate begin, LocalDate end) {
        //1.设置分页参数
        PageHelper.startPage(page, pageSize);
        //2.执行查询操作
        List<Emp> list = empMapper.listCondition(name, gender, begin, end);

        System.out.println(list);
        Page<Emp> p = (Page<Emp>) list;


        //3.封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public void delete(List<Integer> ids) {
        empMapper.deleteByIds(ids);

    }

    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

//    @Override
//    public void update(Emp emp) {
//        emp.setUpdateTime(LocalDateTime.now());
//        empMapper.update(emp);
//    }

    @Override
    public Emp getEmpById(Integer id) {
        Emp emp = empMapper.getEmpById(id);
        return emp;
    }

    @Override
    public void addEmp(Emp emp) {

        emp.setUpdateTime(LocalDateTime.now());
        empMapper.addEmp(emp);
    }


}
